package com.example.employeemanagement.Controller;



import com.example.employeemanagement.dto.DepartmentSalaryDto;
import com.example.employeemanagement.dto.PerformanceTrendDto;
import com.example.employeemanagement.dto.PositionSalaryDto;
import com.example.employeemanagement.dto.SalaryRangeDto;
import com.example.employeemanagement.dto.dashboard.*;
import com.example.employeemanagement.service.DashboardService;
import com.example.employeemanagement.vo.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据大屏控制器
 */
@RestController
@RequestMapping("/dashboard")
public class DashboardController {

    @Autowired
    private DashboardService  dashboardService;

    /**
     * 获取员工总数
     */
    @GetMapping("/employee/count")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getEmployeeCount() {
        try {
            Map<String, Object> data = new HashMap<>();
            data.put("totalCount", dashboardService.getEmployeeCount());
            
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取员工总数成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取员工总数失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
    
    /**
     * 获取本月新增员工数
     */
    @GetMapping("/employee/new-count")
    public ResponseEntity<ApiResponse<Map<String, Object>>> getNewEmployeeCount() {
        try {
            Map<String, Object> data = new HashMap<>();
            data.put("newCount", dashboardService.getNewEmployeeCount());
            
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取新增员工数成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取新增员工数失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取员工部门分布
     */
    @GetMapping("/employee/department-distribution")
    public ResponseEntity<ApiResponse<List<DepartmentDistributionDto>>> getDepartmentDistribution() {
        try {
            List<DepartmentDistributionDto> data = dashboardService.getDepartmentDistribution();
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取员工部门分布成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取员工部门分布失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取员工职位分布
     */
    @GetMapping("/employee/position-distribution")
    public ResponseEntity<ApiResponse<List<PositionDistributionDto>>> getPositionDistribution() {
        try {
            List<PositionDistributionDto> data = dashboardService.getPositionDistribution();
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取员工职位分布成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取员工职位分布失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取考勤状态统计
     */
    @GetMapping("/attendance/status-statistics")
    public ResponseEntity<ApiResponse<List<AttendanceStatusDto>>> getAttendanceStatus() {
        try {
            List<AttendanceStatusDto> data = dashboardService.getAttendanceStatus();
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取考勤状态统计成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取考勤状态统计失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取每日考勤趋势
     */
    @GetMapping("/attendance/daily-trend")
    public ResponseEntity<ApiResponse<List<AttendanceTrendDto>>> getAttendanceTrend(String startDate, String endDate) {
        try {
            List<AttendanceTrendDto> data;
            if (startDate != null && endDate != null) {
                data = dashboardService.getAttendanceTrend(startDate, endDate);
            } else {
                data = dashboardService.getAttendanceTrend();
            }
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取每日考勤趋势成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取每日考勤趋势失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取部门考勤对比
     */
    @GetMapping("/attendance/department-comparison")
    public ResponseEntity<ApiResponse<List<AttendanceTrendDto>>> getDepartmentAttendance() {
        try {
            List<AttendanceTrendDto> data = dashboardService.getDepartmentAttendance();
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取部门考勤对比成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取部门考勤对比失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取绩效分数分布
     */
    @GetMapping("/performance/score-distribution")
    public ResponseEntity<ApiResponse<PerformanceScoreDto>> getPerformanceScore() {
        try {
            PerformanceScoreDto data = dashboardService.getPerformanceScore();
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取绩效分数分布成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取绩效分数分布失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取部门平均绩效
     */
    @GetMapping("/performance/department-average")
    public ResponseEntity<ApiResponse<List<DepartmentPerformanceDto>>> getDepartmentPerformance(
            @RequestParam(required = false) Integer year,
            @RequestParam(required = false) Integer quarter) {
        try {
            List<DepartmentPerformanceDto> data;
            if (year != null && quarter != null) {
                data = dashboardService.getDepartmentPerformance(year, quarter);
            } else {
                data = dashboardService.getDepartmentPerformance();
            }
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取部门平均绩效成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取部门平均绩效失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取绩效趋势
     */
    @GetMapping("/performance/trend")
    public ResponseEntity<ApiResponse<List<PerformanceTrendDto>>> getPerformanceTrend() {
        try {
            List<PerformanceTrendDto> data = dashboardService.getPerformanceTrend();
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取绩效趋势成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取绩效趋势失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取薪资区间分布
     */
    @GetMapping("/salary/range-distribution")
    public ResponseEntity<ApiResponse<List<SalaryRangeDto>>> getSalaryDistribution() {
        try {
            List<SalaryRangeDto> data = dashboardService.getSalaryDistribution();
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取薪资区间分布成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取薪资区间分布失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取部门平均薪资
     */
    @GetMapping("/salary/department-average")
    public ResponseEntity<ApiResponse<List<DepartmentSalaryDto>>> getDepartmentSalary() {
        try {
            List<DepartmentSalaryDto> data = dashboardService.getDepartmentSalary();
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取部门平均薪资成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取部门平均薪资失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 获取职位平均薪资
     */
    @GetMapping("/salary/position-average")
    public ResponseEntity<ApiResponse<List<PositionSalaryDto>>> getPositionSalary() {
        try {
            List<PositionSalaryDto> data = dashboardService.getPositionSalary();
            return new ResponseEntity<>(new ApiResponse<>("SUCCESS", "获取职位平均薪资成功", data), HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(new ApiResponse<>("ERROR", "获取职位平均薪资失败: " + e.getMessage(), null), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}